package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.TbContentCategory;;
import com.dongdongshop.service.TbContentCategoryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("category")
public class TbContentCategoryController {

    private Logger logger = LoggerFactory.getLogger(TbContentCategoryController.class);

    @DubboReference
    private TbContentCategoryService categoryService;

    @RequestMapping("show")
    public String show(){
        return "admin/content_category";
    }



    @RequestMapping("listCategroy")
    @ResponseBody
    public Result listCategroy(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "3") Integer pageSize,
                          String name){
        try {
            PageResult<TbContentCategory> listCategroy = categoryService.listCategroy(pageNum,pageSize,name);
            return Result.OK().setData(listCategroy);
        }catch (Exception e){
            logger.info("查询广告分类出现异常: {}",e);
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
    @RequestMapping("addCategroy")
    @ResponseBody
    public Result addCategroy(TbContentCategory category){
        try {
            categoryService.addCategroy(category);
            return Result.OK();
        }catch (Exception e){
            logger.info("查询广告分类出现异常: {}",e);
            return Result.response(ResultEnum.ADD_MESSAGE_ERROE);
        }
    }
    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){
        try {
            TbContentCategory category = categoryService.selectCatgoryById(id);
            return Result.OK().setData(category);
        }catch (Exception e){
            logger.info("查询广告分类出现异常: {}",e);
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
    @RequestMapping("updateCategroy")
    @ResponseBody
    public Result updateCategroy(TbContentCategory category){
        try {
            categoryService.updateCategroy(category);
            return Result.OK();
        }catch (Exception e){
            logger.info("查询广告分类出现异常: {}",e);
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
    @RequestMapping("deleteCategorys")
    @ResponseBody
    public Result deleteCategorys(Long[] ids){
        try {
            categoryService.deleteCategorys(ids);
            return Result.OK();
        }catch (Exception e){
            logger.info("查询广告分类出现异常: {}",e);
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
}
